#! /bin/bash

# 重复运行k次实验，用法: ./run.sh 5
K=${1:-1}

# 创建总日志文件
START_TIME=$(TZ='Asia/Shanghai' date +"%m%d_%H%M%S")
MAIN_LOG="/public/hcshi/coding/0904label_shift_FL/run_${K}_times_${START_TIME}.log"

# 后台运行整个循环
{
    echo "Started $K experiments at $(date)"
    
    for i in $(seq 1 $K); do
        echo "Running experiment $i/$K at $(date)..."
        TIMESTAMP=$(TZ='Asia/Shanghai' date +"%m%d_%H%M%S")
        OUTPUT_FILE="output_${TIMESTAMP}.log"
        
        echo "  Output file: $OUTPUT_FILE"
        
        /public/hcshi/anaconda3/bin/python \
            -u /public/hcshi/coding/0904label_shift_FL/main_federated.py \
            > /public/hcshi/coding/0904label_shift_FL/$OUTPUT_FILE 2>&1
        
        echo "Experiment $i/$K completed at $(date). Results saved to: $OUTPUT_FILE"
        sleep 3  # 等待GPU释放显存
    done
    
    echo "All $K experiments completed at $(date)!"
} > "$MAIN_LOG" 2>&1 &

echo "Started $K experiments in background. Progress log: run_${K}_times_${START_TIME}.log"